package com.nl.controller.web.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.bean.PersonFlows.req.PersonFlowsReq;
import com.nl.controller.web.bean.PersonFlows.resp.InfoData;
import com.nl.controller.web.bean.sendLimit.QxjSendScope;
import com.nl.controller.web.bean.sendLimit.SendLimitReq;
import com.nl.controller.web.service.LoginService;
import com.nl.controller.web.service.SmsInterfacesService;

import lombok.extern.slf4j.Slf4j;
/**
 * desc:应急短信接口
 * @author syl
 * @Date 2019年7月17日 下午5:07:24
 */
@RestController
@Slf4j
@RequestMapping("smsInterfaces")
public class SmsInterfacesController {
	 @Autowired
	 private SmsInterfacesService smsInterfacesService;
	 
	 @Autowired
	 private LoginService loginService;
	 
	 @Autowired
	 private CommonService commonService;
	 /**
	  * desc:人流量查询接口
	  * @author syl
	  * @Date 2019年7月19日 上午10:45:00
	  * @param personFlowsReq
	  * @return
	  */
	 @RequestMapping(value = "/qryPersonFlows", method = RequestMethod.POST)
	 public List<InfoData> qryPersonFlows(@RequestBody PersonFlowsReq personFlowsReq){
		 List<InfoData> infoDataList = new ArrayList<InfoData>();
		 String flag = "";
		 String result = "";
		 JSONObject json = new JSONObject();
		 json.put("scopeData", personFlowsReq.getScopeData());
		 if (!"".equals(personFlowsReq.getRadius()) && null!=personFlowsReq.getRadius()) {
			 json.put("radius", personFlowsReq.getRadius());
		 }
		 long startTime=System.currentTimeMillis();
		 Map<String, Object> map = smsInterfacesService.qryPersonFlows(json);
		 long endTime=System.currentTimeMillis();
         float excTime=(float)(endTime-startTime)/1000;
         log.info("【人流量查询接口】获取返回结果执行时间：{}",excTime+"s");
		 infoDataList = (List<InfoData>) map.get("infoData");
		 flag = (String) map.get("retCode");
		 if("0".equals(flag)){
			 result="调用成功";
		 }else if("-1".equals(flag)){
			 result="调用失败";
			 log.error((String) map.get("retMsg"));
		 }
		 log.info("【人流量查询接口】入参：{}", json);
		 log.info("【人流量查询接口】是否调用成功：{}", result);
		 //log.info("人流量查询接口返回数据：{}", map);
		 return infoDataList;
	 }
	 /**
	  * desc:项目发送范围同步接口
	  * @author syl
	  * @Date 2019年7月22日 下午5:09:04
	  * @param sendLimitReq
	  * @return
	  */
	 @RequestMapping(value = "/sendMgrLimit", method = RequestMethod.POST,produces="application/json; utf-8")
	 public Map<String, Object> sendMgrLimit(@RequestBody SendLimitReq sendLimitReq){
		 String flag = "";
		 String result = "";
		 JSONObject json = (JSONObject) JSON.toJSON(sendLimitReq);
		 log.info("【项目发送范围同步接口】入参：{}",json);
		 long startTime=System.currentTimeMillis();
		 Map<String, Object> map = smsInterfacesService.sendMgrLimit(json,"2");//页面进入
		 long endTime=System.currentTimeMillis();
         float excTime=(float)(endTime-startTime)/1000;
         log.info("【项目发送范围同步接口】获取返回结果执行时间：{}",excTime+"s");
		 flag = (String) map.get("retCode");
		 if("0".equals(flag)){
			 result="调用成功";
		 }else{
			 result="调用失败";
			 log.error((String) map.get("retMsg"));
		 }
		 log.info("【项目发送范围同步接口】是否调用成功：{}", result);
		 log.info("项目发送范围同步接口返回数据：{}", map);
		 return map;
	 }
	 
	 /**
	  * desc:区县字典同步接口
	  * @author syl
	  * @Date 2019年8月16日 上午9:42:15
	  * @param request
	  * @return
	  */
	 @RequestMapping(value = "/cityCountySyn")
	 public Map<String, Object> cityCountySyn(
			 //@RequestParam(value = "cityCountyList", required = true) String cityCountyList
			 HttpServletRequest request
			 ){
		 InputStream in;
		 String reqJson="";
		try {
			in = request.getInputStream();
			reqJson = IOUtils.toString(in, "utf-8");
			log.info("cityCountyList:"+reqJson);
		} catch (IOException e) {
			e.printStackTrace();
			log.error(e.toString());
		}
		Map<String, Object> map = smsInterfacesService.cityCountySyn(reqJson);
		log.info("【区县字典同步接口】返回数据：{}", map);
		return map;
	 }
	 /*@RequestMapping(value = "/cityCountySyn")
	 public Map<String, Object> cityCountySyn(
			 @RequestParam(value = "cityCountyList", required = true) String cityCountyList){
		Map<String, Object> map = smsInterfacesService.cityCountySyn(cityCountyList);
		log.info("【区县字典同步接口】返回数据：{}", map);
		return map;
	 }*/
	 /**
	  * desc:任务信息同步接口
	  * @author syl
	  * @Date 2019年9月9日 上午10:29:26
	  * @param request
	  * @return
	  */
	 @RequestMapping(value = "/taskDetailSyn")
	 public Map<String, Object> taskDetailSyn(
			 //@RequestParam(value = "taskDetailSynReq", required = true) String taskDetailSynReq
			 HttpServletRequest request){
		/*Map<String, Object> map = smsInterfacesService.taskDetailSyn(taskDetailSynReq);
		log.info("【任务信息同步接口】返回数据：{}", map);
		return map;*/
		
		InputStream in;
		 String reqJson="";
		try {
			in = request.getInputStream();
			reqJson = IOUtils.toString(in, "utf-8");
			log.info("taskDetailSynReq:"+reqJson);
		} catch (IOException e) {
			e.printStackTrace();
			log.error(e.toString());
		}
		Map<String, Object> map = smsInterfacesService.taskDetailSyn(reqJson);
		log.info("【任务信息同步接口】返回数据：{}", map);
		return map;
	 }
	 /**
	  * desc:鉴权,转发请求
	  * @author zhangh
	  * @param request
	  * @param response
	  */
	 @RequestMapping(value = "/authentication")
	 public String authentication(HttpServletRequest request,HttpServletResponse response,HttpSession session
			 
//			,@RequestParam(value = "reqJson", required = true) String reqJson
			 ) {
		 InputStream in;
		 String reqJson="";
		try {
			in = request.getInputStream();
			reqJson = IOUtils.toString(in, "utf-8");
			log.info("鉴权入参:{}"+reqJson);
		} catch (IOException e) {
			e.printStackTrace();
			log.error(e.toString());
		}
		Map<String, String> map = smsInterfacesService.authentication(reqJson);
		String redirect  = "";
		//鉴权通过
		try {
			if ("1".equals( map.get("isPass"))) {
				String cityId = map.get("cityId");
				String qxjCityName = smsInterfacesService.findQXJCityName(cityId);
				String cityName = "";
				cityName = qxjCityName.substring(0, 3);
				String yjCityId = commonService.toCityId(cityName);
				qxjCityName = URLEncoder.encode(qxjCityName, "UTF-8");
				qxjCityName = URLEncoder.encode(qxjCityName, "UTF-8");
				String scopeId = map.get("scopeId");
				String flag = map.get("flag");
				SmsUsers user = loginService.login("15150570249", "123456");
				session.setAttribute("user", user);
				String requestFlag = map.get("requestFlag");
				if ("1".equals(requestFlag)) {//政务网
					//0是圈画地图
					if ("0".equals(flag)) {
						redirect =  "/view/areaManage/mapManage3.html?cityName="+qxjCityName+"&scopeId="+scopeId+"&cityId="+yjCityId;
					}else if("1".equals(flag)){//1是审核查看地图
						redirect = "/view/areaManage/mapManageView3.html?cityName="+qxjCityName+"&scopeId="+scopeId;
					}else {
						redirect = "/view/areaManage/invalidToken.html";
					}
				}else {//气象局
					//0是圈画地图
					if ("0".equals(flag)) {
						redirect =  "/view/areaManage/mapManage2.html?cityName="+qxjCityName+"&scopeId="+scopeId+"&cityId="+yjCityId;
					}else if("1".equals(flag)){//1是审核查看地图
						redirect = "/view/areaManage/mapManageView2.html?cityName="+qxjCityName+"&scopeId="+scopeId;
					}else {
						redirect = "/view/areaManage/invalidToken.html";
					}
				}
				log.info("鉴权成功,跳转至"+redirect);
			}else {
				redirect ="/view/areaManage/invalidToken.html";
				log.info("鉴权失败,跳转至"+redirect);
			}
//			redirect = URLEncoder.encode(redirect);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return redirect;
	}
	
	 /**
	  * desc:气象局圈划范围同步接口
	  * @author ll
	  * @Date 2019年11月18日 下午3:40:04
	  * @param qxjSendScope
	  * @return
	  */
	 @RequestMapping(value = "/synScope", method = RequestMethod.POST,produces="application/json; utf-8")
	 public Map<String, Object> synScope(@RequestBody QxjSendScope qxjSendScope){
		 String flag = "";
		 String result = "";
		 JSONObject json = (JSONObject) JSON.toJSON(qxjSendScope);
		 log.info("【气象局圈划范围同步接口】入参：{}",json);
		 long startTime=System.currentTimeMillis();
		 Map<String, Object> map = new HashMap<String, Object>();
		 map = 	 smsInterfacesService.synScope(json);
		 long endTime=System.currentTimeMillis();
         float excTime=(float)(endTime-startTime)/1000;
         log.info("【气象局圈划范围同步接口】获取返回结果执行时间：{}",excTime+"s");
		 flag = (String) map.get("retCode");
		 if("0".equals(flag)){
			 result="调用成功";
		 }else if("-1".equals(flag)){
			 result="调用失败";
			 log.error((String) map.get("retMsg"));
		 }
		 log.info("【气象局圈划范围同步接口】入参：{}",json);
		 log.info("【气象局圈划范围同步接口】是否调用成功：{}", result);
		 //log.info("项目发送范围同步接口返回数据：{}", map);
		 return map;
	 }
}
